﻿using System;
using MyMenu.action;
using System.IO;
using System.Xml.Serialization;
using MediaPortal.Configuration;


namespace MyMenu.config
{

    public class XmlParseur
    {

        public static action.Action getFirstAction(String path)
        {
            StreamReader str = null;
            try
            {
                str = new StreamReader(path);
                XmlSerializer xSerializer = new XmlSerializer(typeof(serial.action));
                serial.action action = (serial.action)xSerializer.Deserialize(str);

                if (action == null)
                {
                    action.Action myAction = new ShowMenu();
                    myAction.name = "New Menu";
                    return myAction;
                }
                else
                {
                    return toAction(action, null);
                }
            }
            catch (Exception e)
            {
                action.Action myAction = new ShowMenu();
                myAction.name = "New Menu";
                return myAction;
            }
            finally
            {
                if (str != null)
                {
                    str.Close();
                }
            }
        }
      

        public static void toXml(action.Action action, string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(serial.action));
            TextWriter tw = new StreamWriter(path);
            serializer.Serialize(tw, toAction(action));
            tw.Close();
        }

        private static serial.action toAction(action.Action myAction)
        {
            serial.action action = new serial.action();
            action.name = myAction.name;
            action.KeyChar = myAction.keyChar;
            if (myAction is ShowMenu)
            {
                action.type = "showMenu";
                action.actions = toAction((ShowMenu)myAction);
            }
            else if (myAction is ExecuteCommand)
            {
                action.type = "executeCommand";
                action.command = ((ExecuteCommand)myAction).command;
                action.args = ((ExecuteCommand)myAction).param;
                action.hidden = ((ExecuteCommand)myAction).hidden.ToString();

            }
            else if (myAction is ActivateWindowCommand)
            {
                action.type = "activateWindow";
                action.windowId = ((ActivateWindowCommand)myAction).id;
             

            }
            else if (myAction is ConfirmedAction)
            {
                action = toAction(((ConfirmedAction)myAction).action);
                action.confirmation = Boolean.TrueString;
            }
            return action;
        }


        private static serial.action[] toAction(ShowMenu action)
        {
            serial.action[] actions = new serial.action[action.actions.Length];
            for (int i = 0; i < actions.Length; i++)
            {
                actions[i] = toAction(action.actions[i]);
            }
            return actions;
        }

        private static action.Action toAction(serial.action action, action.Action actionBack)
        {
            action.Action myAction = null;

            string type = action.type;
            if ("showMenu".Equals(type))
            {
                myAction = new action.ShowMenu();
                myAction.name = action.name;
                myAction.keyChar = action.KeyChar;
                if (action.actions != null)
                {
                    ((ShowMenu)myAction).actions = getActions(action, myAction);
                }
            }
            else if ("executeCommand".Equals(type))
            {
                bool hidden = bool.TryParse(action.hidden, out hidden) ? hidden : false;
                myAction = new action.ExecuteCommand(action.command, action.args, hidden);
                myAction.name = action.name;
            }
            else if ("activateWindow".Equals(type))
            {
                myAction = new action.ActivateWindowCommand(action.windowId);
                myAction.name = action.name;
            }
            if (bool.TrueString.Equals(action.confirmation))
            {
                myAction = new action.ConfirmedAction(myAction, actionBack);
                myAction.name = action.name;
            }
            return myAction;
        }

        private static action.Action[] getActions(serial.action action, action.Action actionBack)
        {
            action.Action[] actions = new action.Action[action.actions.Length];
            for (int i = 0 ; i<actions.Length ; i++)
            {
                actions[i] = toAction(action.actions[i], actionBack);
            }
            return actions;
        }
    }
}
